/*
 * AddOrderDeliveryController.java
 * Created on 4-Apr-2011, 12:16:18 PM
 */
package pos.control;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import pos.entity.Item;
import pos.entity.Order;
import pos.entity.ShipInfo;
import pos.interfaces.Database;
import pos.storage.StorageFactory;
import pos.storage.db.sql.SQL;
import pos.util.IDGenerator;

/**
 * @since 30 MARCH 2011, 5:00 PM
 * @author Anup Mathew
 * <b>Description:<b> <br>This Class is used as a controller for the AddOrderDelivery
 * use case. This class is used by the Add Delivery address UI,
 * to record the delivery information and return the final delivery
 * charges.
 */
public class AddOrderDeliveryController {

    private ShipInfo shipInfo;
    private Order order;

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param shippingInfo
     * @param order 
     */
    public static void addDeliveryDetails(ShipInfo shippingInfo, Order order) {
        validate(shippingInfo);
        //getShippingCharges(shippingInfo);
        storeToDeliveryInfo(shippingInfo, order);
        
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param shippingInfo 
     */
    private static void validate(ShipInfo shippingInfo) {
        
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param shippingInfo 
     */
    private static void getShippingCharges(ShipInfo shippingInfo) {
          try {
            Database mysql = (Database) StorageFactory.getStorage(StorageFactory.DBTYPE, Database.MYSQL);
            Connection custConn = mysql.getConnection(Database.CUSTOMER);
            
            String sqlQuery = SQL.GET_SHIPPING_CHARGE;

            PreparedStatement ps = (PreparedStatement) custConn.prepareStatement(sqlQuery);
            ps.setString(1, shippingInfo.getZip());

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                shippingInfo.setPrice(rs.getDouble("ShippingPrice"));
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } 
    }
    
    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param item
     * @param orderId 
     */
    private static void storeToDeliveryInfo(ShipInfo shipInfo, Order order) {
       try {
            Database mysql = (Database) StorageFactory.getStorage(StorageFactory.DBTYPE, Database.MYSQL);
            Connection custConn = mysql.getConnection(Database.CUSTOMER);
            
            String sqlQuery = SQL.INSERT_DELIVERY_INFO;

            PreparedStatement ps = (PreparedStatement) custConn.prepareStatement(sqlQuery);
            ps.setLong(1, IDGenerator.getID());
            //ps.setLong(2, order.getOrderId());
            ps.setInt(2, 2);
            ps.setString(3, shipInfo.getAddress());
            ps.setString(4, shipInfo.getZip());
            ps.setString(5, shipInfo.getCity());
            ps.setString(6, shipInfo.getCountry());
            ps.setLong(7, shipInfo.getMobile());
            ps.setInt(8, 2);
            ps.setDate(9, new Date(System.currentTimeMillis()));
            ps.setDate(10, new Date(System.currentTimeMillis()));

            Boolean returnValue = ps.execute();

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param shippingInfo
     * @param order 
     */
    /*
    private static void storeShippingInfo(ShipInfo shippingInfo, Order order) {
        System.out.println("33333333333333333333333");
        
        Database mysql = (Database) StorageFactory.getStorage(StorageFactory.DBTYPE, Database.MYSQL);
            Connection custConn = mysql.getConnection(Database.CUSTOMER);
            
            String sqlQuery = SQL.INSERT_DELIVERY_INFO;

            PreparedStatement ps = (PreparedStatement) custConn.prepareStatement(sqlQuery);
            //ps.setInt(1, order.getItems().size());
            ps.setString(2, shippingInfo.getAddress());
            ps.setString(3, shippingInfo.getZip());
            ps.setString(4, shippingInfo.getCity());
            ps.setString(5, shippingInfo.getCountry());
            ps.setLong(6, shippingInfo.getMobile());
            ps.setDouble(7, shippingInfo.getPrice());
            ps.setDate(4, new Date(System.currentTimeMillis()));

            Boolean returnValue = ps.execute();
    }
    */
    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param shippingInfo
     * @param order 
     */
    /*
     private static void storeDeliveryManifest(ShipInfo shippingInfo, Order order) {
        try {
            System.out.println("-----------------");
            

            if(!returnValue){
                //raiseException;
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    */
    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @return 
     */
    public Order getOrder() {
        return order;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param order 
     */
    public void setOrder(Order order) {
        this.order = order;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @return 
     */
    public ShipInfo getShipInfo() {
        return shipInfo;
    }

    /**
     * @since 30 MARCH 2011, 5:00 PM
     * @param shipInfo 
     */
    public void setShipInfo(ShipInfo shipInfo) {
        this.shipInfo = shipInfo;
    }
}
